import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
*Klasa odpowiedzialna za stworzenie GUI i obsluge polaczenia
**/
class Player extends Frame implements ActionListener {
    JLabel output; // pole do wyswietlania danych
    JLabel adress;
    JLabel Port;
    JLabel Exchange;
    JLabel ID;
    JLabel Pot;
    JLabel Poto;
    JLabel Maxbet;
    JLabel Maxbeto;
    JLabel IDo;
    JLabel casho;
    JLabel cash;
    JButton rise;
    JButton send;
    JButton get;
    JButton result;
    JButton card1;
    JButton card2;
    JButton card3;
    JButton card4;
    JButton Exchangeb;
    JButton bet;
    JButton allin;
    JButton ready;
    JButton check;
    JButton start;
    JButton fold;
    JPanel panel;
    JButton login; // Przycisk logujacy na server
    JTextField betinput; // Pole do wprowadzania danych
    JTextField name; // pole do wprowadzenia adresu
    JTextField port; // pole do wprowadzenia numeru portu
    Socket socket = null; // obiekt socketu
    PrintWriter out = null; // metoda wysylania danych
    BufferedReader in = null; // metoda pobieania danych
    static int pr;
    static String sr;
    String[] help =null;
    int playercash;
    int maxbet1=0;
    int maxbet2=0;
    String Allin="0";
    int exbt=0;
    /**
    *Konstruktor klasy socket tworzacy GUI
    **/
   Player() {
        setBounds(400,100,1000,800);
        setFont(new Font(Font.SANS_SERIF,Font.PLAIN,40));
        betinput = new JTextField(20);
        betinput.setBounds(390,160,365,30);
        name = new JTextField("localhost");
        name.setBounds(200,25,150,30);
        port = new JTextField("4444");
        port.setBounds(200,60,150,30);
        panel=new JPanel();
        panel.setLayout(null);
        output = new JLabel();
        output.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        output.setOpaque(true);
        output.setBounds(390,25,365,100);
        output.setBackground(Color.white);
        adress = new JLabel("Adress");
        adress.setBounds(80,25,150,30);
        Port = new JLabel("Port");
        Port.setBounds(85,60,150,30);
        Pot = new JLabel("Pot");
        Pot.setBounds(150,160,50,30);
        ID = new JLabel("ID");
        ID.setBounds(50,160,50,30);
        IDo = new JLabel();
        IDo.setOpaque(true);
        IDo.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        IDo.setBounds(80,160,50,30);
        IDo.setBackground(Color.white);
        Poto = new JLabel();
        Poto.setOpaque(true);
        Poto.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        Poto.setBounds(180,160,50,30);
        Poto.setBackground(Color.white);
        Maxbet = new JLabel("Maxbet");
        Maxbet.setBounds(250,160,50,30);
        Maxbeto = new JLabel();
        Maxbeto.setOpaque(true);
        Maxbeto.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        Maxbeto.setBounds(300,160,50,30);
        Maxbeto.setBackground(Color.white);
        rise = new JButton("Rise");
        rise.setBounds(460,200,60,30);
        rise.addActionListener(this);
        login = new JButton("Login");
        login.setBounds(25,95,150,30);
        login.addActionListener(this);
        bet = new JButton("Bet");
        bet.setBounds(380,200,60,30);
        bet.addActionListener(this);
        check = new JButton("Check");
        check.setBounds(540,200,70,30);
        check.addActionListener(this);
        allin = new JButton("Allin");
        allin.setBounds(630,200,60,30);
        allin.addActionListener(this);
        fold = new JButton("Fold");
        fold.setBounds(710,200,60,30);
        fold.addActionListener(this);
        card1 = new JButton();
        card1.setBounds(65,250,150,200);
        card1.addActionListener(this);
        card2 = new JButton();
        card2.setBounds(230,250,150,200);
        card2.addActionListener(this);
        card3 = new JButton();
        card3.setBounds(395,250,150,200);
        card3.addActionListener(this);
        card4 = new JButton();
        card4.setBounds(560,250,150,200);
        card4.addActionListener(this);
        Exchange = new JLabel("");
        Exchange.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        Exchange.setOpaque(true);
        Exchange.setBounds(375,460,250,30);
        Exchange.setBackground(Color.white);
        Exchangeb = new JButton("Exchange");
        Exchangeb.setBounds(115,460,250,30);
        Exchangeb.addActionListener(this);
        cash = new JLabel("Cash");
        cash.setBounds(150,200,250,30);
        casho = new JLabel();
        casho.setOpaque(true);
        casho.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
        casho.setBounds(200,200,50,30);
        casho.setBackground(Color.white);
        send = new JButton("Send");
        send.setBounds(170,500,100,30);
        send.addActionListener(this);
        ready = new JButton("Ready");
        ready.setBounds(280,500,100,30);
        ready.addActionListener(this);
        start = new JButton("Start");
        start.setBounds(390,500,100,30);
        start.addActionListener(this);
        get = new JButton("Get Cards");
        get.setBounds(500,500,100,30);
        get.addActionListener(this);
        result = new JButton("Result");
        result.setBounds(610,500,100,30);
        result.addActionListener(this);
        


        setLayout(new BorderLayout());
        add(panel);
        panel.add(adress);
        panel.add(Port);
        panel.add(name);
        panel.add(port);
        panel.add(login);
        panel.add(betinput);
        panel.add(rise);
        panel.add(output);
        panel.add(Pot);
        panel.add(ID);
        panel.add(IDo);
        panel.add(Maxbet);
        panel.add(Poto);
        panel.add(Maxbeto);
        panel.add(bet);
        panel.add(check);
        panel.add(allin);
        panel.add(fold);
        panel.add(card1);
        panel.add(card2);
        panel.add(card3);
        panel.add(card4);
        panel.add(Exchange);
        panel.add(Exchangeb);
        panel.add(cash);
        panel.add(casho);
        panel.add(send);
        panel.add(ready);
        panel.add(start);
        panel.add(get);
        panel.add(result);
        
    }
    /**
    *Metoda sterujaca akcja przyciskow
    **/
    public void actionPerformed(ActionEvent event) {
        /**
        *Akcja przycisku Send
        **/
       
        /**
        *Akcja przycisku Login
        **/
         if(event.getSource() == login)
            {
            sr=name.getText();
            pr=Integer.parseInt(port.getText());
            listenSocket(sr,pr);
            try {
            output.setText(in.readLine());
            IDo.setText(in.readLine());
            casho.setText(in.readLine());
            Poto.setText("0");
            Maxbeto.setText("0");
            playercash=Integer.parseInt(casho.getText());
            output.setText(in.readLine());
            
            }
            catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
            
        }
         else if(event.getSource() == ready){
        	 try {
        		 out.println(IDo.getText());
        		 output.setText(in.readLine());

        		 
        		 
        	 }
             catch (IOException e) {
                 System.out.println("Read failed"); System.exit(1);
             }
         }
         else if(event.getSource() == start){
        	 try {
                 bet.setEnabled(true);
                 rise.setEnabled(true);
                 fold.setEnabled(true);
                 check.setEnabled(true);
        		 out.println(IDo.getText());
        		 help=in.readLine().split(" ");
                 if((help[0].equals("B"))||(help[0].equals("S"))){
                 	playercash=playercash-Integer.parseInt(help[1]);
                 	casho.setText(""+playercash);
                 }
                 output.setText(help[2]+" "+help[3]);
                 Poto.setText(in.readLine());
                 help=null;
                 
        	 }
             catch (IOException e) {
                 System.out.println("Read failed"); System.exit(1);
             }
         }
         else if(event.getSource() == get){
        	 try{
        	 help=in.readLine().split(" ");
             card1.setText(help[0]);
             card2.setText(help[1]);
             card3.setText(help[2]);
             card4.setText(help[3]);
             Poto.setText(in.readLine());
             Maxbeto.setText(in.readLine());
             output.setText(in.readLine());
        	 }
        	 catch (IOException e) {
                 System.out.println("Read failed"); System.exit(1);
             }
         }
        else if(event.getSource() == bet){
        	try {
        		if(Integer.parseInt(Maxbeto.getText())>Integer.parseInt(casho.getText())){
        			output.setText("You don't have enough money");
        		}
        		else if((Maxbeto.getText().equals(0)))
        			output.setText("You must bet something");
        		else{
        	out.println("bet "+Maxbeto.getText());
        	playercash=playercash-Integer.parseInt(Maxbeto.getText());
        	casho.setText(""+playercash);
            output.setText(in.readLine());
        		}

        	}
            catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
        }
        else if(event.getSource() == rise){
        	try {
        	if((betinput.getText().equals(""))||Integer.parseInt(betinput.getText())<=Integer.parseInt(Maxbeto.getText())){
        		output.setText("You must rise bet");
        	}
        	else{
        	out.println("rise "+betinput.getText());	
        	Maxbeto.setText(betinput.getText());
        	playercash=playercash-Integer.parseInt(betinput.getText());
        	casho.setText(""+playercash);
            output.setText(in.readLine());

        	}
        	}
            catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
        }
        else if(event.getSource() == check){
        	try {
        
        	if(maxbet1==maxbet2){
        	out.println("check "+"0");
        	maxbet1=maxbet2;
            output.setText(in.readLine());

        	}
        	
            
        	
        	else
        		output.setText("You must bet something");
        }
        catch (IOException e) {
            System.out.println("Read failed"); System.exit(1);
        }
       }
        
        else if(event.getSource() == allin){
        	try {
        
        	Allin="1";
        	out.println("Allin "+playercash);
        	playercash=0;
        	casho.setText(""+playercash);
            output.setText(in.readLine());

            bet.setEnabled(false);
            rise.setEnabled(false);
            fold.setEnabled(false);
            check.setEnabled(false);
        	}
            catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
        }
        else if(event.getSource() == fold){
        	try {
        
        	out.println("Fold "+"0");
        	output.setText("game over");
            output.setText(in.readLine());
            in.close();
            out.close();

        	  }
            catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
        }
        else if(event.getSource() == card1){
        	Exchange.setText(Exchange.getText()+card1.getText()+" ");
        	card1.setText("X");
        	card1.setEnabled(false);
        }
        else if(event.getSource() == card2){
        	Exchange.setText(Exchange.getText()+card2.getText()+" ");
        	card2.setText("X");
        	card2.setEnabled(false);
        }
        else if(event.getSource() == card3){
        	Exchange.setText(Exchange.getText()+card3.getText()+" ");
        	card3.setText("X");
        	card3.setEnabled(false);
        }
        else if(event.getSource() == card4){
        	Exchange.setText(Exchange.getText()+card4.getText()+" ");
        	card4.setText("X");
        	card4.setEnabled(false);
        }
        else if(event.getSource() == Exchangeb){
        	try{
        		if(exbt!=2){
        		out.println(Exchange.getText());
        		if(!Exchange.getText().equals("")){
        		System.out.println(Exchange.getText());
        		Exchange.setText("");
        		help=in.readLine().split(" ");
        		for(int i=0; i<help.length; i++)
        			System.out.println(help[i]);
        		for(int i=0; i<help.length; i++){
        			if(card1.getText()=="X"){
        				card1.setText(help[i]);
        				card1.setEnabled(true);

        			}
        			else if(card2.getText()=="X"){
        				card2.setText(help[i]);
        				card2.setEnabled(true);

        			}
        			else if(card3.getText()=="X"){
        				card3.setText(help[i]);
        				card3.setEnabled(true);

        			}
        			else if(card4.getText()=="X"){
        				card4.setText(help[i]);
        				card4.setEnabled(true);

        			}

        			
                   
        		}
        		}
                maxbet2=Integer.parseInt(Maxbeto.getText());
        		Poto.setText(in.readLine());
                Maxbeto.setText(in.readLine());
    			output.setText(in.readLine());

                exbt++;
        		}
                
        		else{
             		out.println(Exchange.getText());
            		if(!Exchange.getText().equals("")){
            		System.out.println(Exchange.getText());
            		Exchange.setText("");
            		help=in.readLine().split(" ");
            		for(int i=0; i<help.length; i++)
            			System.out.println(help[i]);
            		for(int i=0; i<help.length; i++){
            			if(card1.getText()=="X"){
            				card1.setText(help[i]);
            				card1.setEnabled(true);

            			}
            			else if(card2.getText()=="X"){
            				card2.setText(help[i]);
            				card2.setEnabled(true);

            			}
            			else if(card3.getText()=="X"){
            				card3.setText(help[i]);
            				card3.setEnabled(true);

            			}
            			else if(card4.getText()=="X"){
            				card4.setText(help[i]);
            				card4.setEnabled(true);

            			}

            			
                       
            		}
            		}
            		maxbet2=Integer.parseInt(Maxbeto.getText());
            		Poto.setText(in.readLine());
                    Maxbeto.setText(in.readLine());
        			output.setText(in.readLine());

                }
        	}
        	 catch (IOException e) {
                 System.out.println("Read failed"); System.exit(1);
             }
        	
        }
        else if(event.getSource() == send){
        	try{
        	out.println(card1.getText()+" "+card2.getText()+" "+card3.getText()+" "+card4.getText());
        	output.setText(in.readLine());

        
        }
        	 
   	 catch (IOException e) {
            System.out.println("Read failed"); System.exit(1);
        }
        }
        else if(event.getSource() == result){
        	try{
            out.println(card1.getText()+" "+card2.getText()+" "+card3.getText()+" "+card4.getText());
        	help=in.readLine().split(" ");
			output.setText(help[0]+" "+help[2]);
        	playercash=playercash+Integer.parseInt(help[1]);
        	casho.setText(""+playercash);
    		Poto.setText(in.readLine());
      	  card1.setText("");
          card2.setText("");
          card3.setText("");
          card4.setText("");
        	}
        	catch (IOException e) {
                System.out.println("Read failed"); System.exit(1);
            }
	 }
    }
    
    /**
    *Metoda odpowiadajaca za polaczenie z serwerem
    **/
    public void listenSocket(String srw,int prt){
        try {
            socket = new Socket(srw,prt);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        catch (UnknownHostException e) {
            System.out.println("Unknown host: localhost"); System.exit(1);
        }
        catch  (IOException e) {
            System.out.println("No I/O"); System.exit(1);
        }
    }
    /**
    *Glowna funkcja programu
    **/
    public static void main(String[] args){
        Player Jframe = new Player();
        Jframe.addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        } );
        Jframe.pack();
        Jframe.setVisible(true);
        Jframe.setSize(800,600);
    }
}
